home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 501-525 / disk_519 / fifolib / test.c < prev   
C/C++ Source or Header  |  1992-05-06  |  3KB  |  152 lines

  1.  
  2. /*
  3.  *  TEST.C
  4.  *
  5.  *  TEST [R/W][N] fifo_name
  6.  *
  7.  *  test fifo operation
  8.  *
  9.  */
  10.  
  11. #include <exec/types.h>
  12. #include <exec/ports.h>
  13. #include <libraries/dos.h>
  14. #include <stdio.h>
  15. #include "fifo.h"
  16.  
  17. typedef struct MsgPort    MsgPort;
  18. typedef struct Message    Message;
  19.  
  20. long FifoBase;
  21. long Fh;
  22. MsgPort *WaPort;
  23. char IBuf[256];
  24.  
  25. void DoWait();
  26. void *CreatePort();
  27.  
  28. void
  29. myexit(void)
  30. {
  31.     if (Fh) {
  32.     CloseFifo(Fh, FIFOF_EOF);
  33.     Fh = 0;
  34.     }
  35.     if (FifoBase) {
  36.     CloseLibrary(FifoBase);
  37.     FifoBase = 0;
  38.     }
  39.     if (WaPort) {
  40.     DeletePort(WaPort);
  41.     WaPort = NULL;
  42.     }
  43. }
  44.  
  45. main(ac, av)
  46. char *av[];
  47. {
  48.     if (ac != 3) {
  49.     fputs("TEST [R/W][N] fifo_name\n", stderr);
  50.     exit(1);
  51.     }
  52.     atexit(myexit);
  53.  
  54.     WaPort = CreatePort(NULL, 0);
  55.  
  56.     if (FifoBase = OpenLibrary(FIFONAME, 0)) {
  57.     long flags = FIFOF_NORMAL;
  58.  
  59.     fputs("fifo.library opened!\n", stderr);
  60.  
  61.     if (av[1][1] == 'N')
  62.         flags |= FIFOF_NBIO;
  63.  
  64.     switch(av[1][0]) {
  65.     case 'R':
  66.         if (Fh = OpenFifo(av[2], 4096, FIFOF_READ | flags)) {
  67.         long i = 0;
  68.         char *ptr;
  69.  
  70.         fprintf(stderr, "fifo is %d bytes\n", BufSizeFifo(Fh));
  71.         for (;;) {
  72.             long n = ReadFifo(Fh, &ptr, i);
  73.             if (n > 64)     /*  limit amount of data read/loop */
  74.             n = 64;
  75.             i = n;
  76.  
  77.             if (n < 0) {
  78.             fputs("EOF\n", stderr);
  79.             break;
  80.             }
  81.             if (n > 0) {
  82.             /*
  83.              *  just so other windows doesn't freeze while we
  84.              *  output kilobytes.
  85.              */
  86.  
  87.             write(1, ptr, n);
  88.             } else {        /*    n == 0    */
  89.             if (flags & FIFOF_NBIO) {
  90.                 DoWait(FREQ_RPEND);
  91.             } else {
  92.                 puts("0 bytes avail?");
  93.             }
  94.             }
  95.             chkabort();
  96.         }
  97.         }
  98.         break;
  99.     case 'W':
  100.         if (Fh = OpenFifo(av[2], 4096, FIFOF_WRITE | FIFOF_KEEPIFD | flags)) {
  101.         long n;
  102.  
  103.         fprintf(stderr, "fifo is %d bytes\n", BufSizeFifo(Fh));
  104.  
  105.         while ((n = read(0, IBuf, sizeof(IBuf))) > 0) {
  106.             long r;
  107.             long x = 5;
  108.  
  109. loop:
  110.             r = WriteFifo(Fh, IBuf, n);
  111.             if (r != n) {
  112.             if (r >= 0 && (flags & FIFOF_NBIO)) {
  113.                 DoWait(FREQ_WAVAIL);
  114.                 goto loop;
  115.             } else {
  116.                 fprintf(stderr, "write failed! %d\n", r);
  117.                 break;
  118.             }
  119.             }
  120.         }
  121.         }
  122.         break;
  123.     default:
  124.         fputs("bad command line", stderr);
  125.         break;
  126.     }
  127.     }
  128.     return(0);
  129. }
  130.  
  131. void
  132. DoWait(req)
  133. long req;
  134. {
  135.     Message msg;
  136.     long mask = 0;
  137.  
  138.     fputs("WAIT\n", stderr);
  139.     msg.mn_ReplyPort = WaPort;
  140.     RequestFifo(Fh, &msg, req);
  141.  
  142.     while (msg.mn_Node.ln_Type == NT_MESSAGE) {
  143.     mask = Wait(SIGBREAKF_CTRL_C | (1 << WaPort->mp_SigBit));
  144.     if (mask & SIGBREAKF_CTRL_C)
  145.         RequestFifo(Fh, &msg, FREQ_ABORT);
  146.     }
  147.     GetMsg(WaPort);
  148.     if (mask & SIGBREAKF_CTRL_C)
  149.     fputs("WAIT: ABORT\n", stderr);
  150. }
  151.  
  152.